#include "StdAfx.h"
#include "../sys/System.h"

#include "../game/Game.h"

CSystem CSystem::__this;

void VBlankIntr()
{
	CSystem::getInstance()->vblank();
}

CSystem::CSystem()
{
	m_videoMode = VIDEOMODE_NONE;
	m_screenPos = MAINSCREEN_DOWN;
	m_targetScreenPos = MAINSCREEN_UP;
	m_brightness = 0;
	m_targetBrightness = 0;
	m_isRendering = FALSE;
}

void CSystem::initSystem()
{
}

void CSystem::setVideoMode(EVideoMode mode)
{
	m_videoMode = mode;
}

void CSystem::setup3D()
{
/*	G3X_SetShading(GX_SHADING_HIGHLIGHT);//GX_SHADING_TOON);

	G3X_AntiAlias(TRUE);

	G3X_AlphaBlend(TRUE);

	G2_SetBlendAlpha(GX_BLEND_PLANEMASK_BG2, GX_BLEND_PLANEMASK_BG0, 15, 31);

	G3_SwapBuffers(GX_SORTMODE_MANUAL, GX_BUFFERMODE_W);

	G3X_SetClearColor(GX_RGB(0, 0, 0),                     // clear color
		0,                                  // clear alpha
		0x7fff,                              // clear depth
		63,                                  // clear polygon ID
		TRUE                                // fog
		);

	G3_ViewPort(0, 0, 255, 191);*/
}

void CSystem::setFOV(int fov)
{
	m_fov = fov;

/*	fx32    right = FX32_ONE;
	fx32    top = FX32_ONE * 3 / 4;
	fx32    near = FX32_ONE;
	fx32    far = FX32_ONE * 400;

	G3_Perspective(FX32_SIN30, FX32_COS30,  // sine and cosine of FOVY
		FX32_ONE * 4 / 3,        // aspect
		near,           // near
		far,            // far
		NULL            // a pointer to a matrix if you use it
		);

	G3_StoreMtx(0);  */ 
}

void CSystem::copyScreens()
{
	u16* mainBuffer;
	u16* subBuffer;

	BOOL changeScreenPos = FALSE;

	if( m_targetScreenPos != m_screenPos )
	{
		m_screenPos = m_targetScreenPos;
		changeScreenPos = TRUE;
	}

	CGame *game = CGame::getInstance();

	if( m_brightness != -16 )
	{
		mainBuffer	= game->getMainBuffer();	
		subBuffer	= game->getSubBuffer();

		GX_VBlankIntr( FALSE );

		//copy secondary screen

		if ( m_videoMode == VIDEOMODE_2D || m_videoMode == VIDEOMODE_2D3D )
		{
			//copy main screen
		}
	}

	if( changeScreenPos )
	{
// 		if (m_screenPos == MAINSCREEN_UP)
// 			GX_SetDispSelect( GX_DISP_SELECT_MAIN_SUB );
// 		else
// 			GX_SetDispSelect( GX_DISP_SELECT_SUB_MAIN );
		game->setMainScreenUp(m_screenPos == MAINSCREEN_UP);
	}

	GX_VBlankIntr( TRUE );
}

void CSystem::clear3DScreen(BOOL flip)
{
}

void CSystem::flip3DScreen()
{
}

void CSystem::vblank()
{
	if ( !m_isRendering )
	{
		CGame::getInstance()->VBlank();

		if( m_brightness != m_targetBrightness )
		{
			m_brightness = m_targetBrightness;
		}
	}
}